# -*- coding: utf-8 -*-

from traceback import format_exc
from .items import MobileNewsItem
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .models import Article

class MobileNewsPipeline(object):

    def __init__(self, db_host, db_name, db_user, db_passwd, db_port):
        # 初始化数据库连接:
        conn_str = 'mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8' % (db_user, db_passwd, db_host, db_port, db_name)
        engine = create_engine(conn_str)
        # 创建DBSession类型:
        self.DBSession = sessionmaker(bind=engine)

    @classmethod
    def from_crawler(cls, crawler):
        settings = crawler.settings
        return cls(
            db_host = settings.get('MYSQL_HOST'),
            db_name = settings.get('MYSQL_DBNAME'),
            db_user = settings.get('MYSQL_USER'),
            db_passwd = settings.get('MYSQL_PASSWD'),
            db_port = settings.get('MYSQL_PORT')
        )

    def open_spider(self, spider):
        _ = spider
        # 通过cursor执行增删查改
        self.session = self.DBSession();

    def close_spider(self, spider):
        _ = spider
        self.session.close()

    def process_item(self, item, spider):
        a_num = self.session.query(Article).filter(Article.url == item['url']).count()
        #item_dict = dict(item)
        if 'time' not in item:
            item['time'] = None
        try:
            if isinstance(item, MobileNewsItem) and a_num == 0:
                article = Article(
                    article_name = item['title'],
                    url = item['url'],
                    logo = item['img_url'],
                    article_time = item['time'],
                    author = item['author'],
                    source = item['source']
                )
                self.session.add(article)
                self.session.commit()
        except Exception as e:
            _ = e
            spider.logger.error(format_exc())
            self.session.rollback()
        return item